package com.chat.simplechat.response;

import com.sun.org.apache.bcel.internal.generic.ObjectType;
import lombok.Data;

import java.util.Map;

/**
 * 消息响应体
 * @author wangyabin
 * @date 2022/8/22 13:34
 */
@Data
public class Result<T> {

    private Result(){}

    public static final int SUCCESS = 200;

    public static final int FAIL = 500;

    public static final String SUCCESS_TEXT = "success";

    public static final String FAIL_TEXT = "fail";

    private Integer code = SUCCESS;

    private String message = SUCCESS_TEXT;

    //附加数据
    private Map<String,Object> attach; 

    private T data;

    public static <E> Result SUCCESS(){
        Result result = Result.builder()
                .code(SUCCESS)
                .message(SUCCESS_TEXT)
                .build();
        return result;
    }

    public static <E> Result SUCCESS(E data,String message){
        Result result = Result.builder()
                .code(SUCCESS)
                .data(data)
                .message(message)
                .build();
        return result;
    }
    
    public static <E> Result SUCCESS(E data,Map<String,Object> attach){
        Result result = Result.builder()
                .code(SUCCESS)
                .data(data)
                .message(SUCCESS_TEXT)
                .attach(attach)
                .build();
        return result;
    }

    public static <E> Result SUCCESS(E data){
        Result result = Result.builder()
                .code(SUCCESS)
                .data(data)
                .message(SUCCESS_TEXT)
                .build();
        return result;
    }

    public static <E> Result FAIL(String message){
        Result result = Result.builder()
                .code(FAIL)
                .message(message)
                .build();
        return result;
    }
    
    private static Builder builder(){
        Builder builder = new Builder();
        builder.target = new Result();
        return builder;
    }

    private static class Builder {
        private Result target;
        public Builder code(Integer code){
            target.code = code;
            return this;
        }
        public Builder message(String message){
            target.message = message;
            return this;
        }
        public <T> Builder data(T data){
            target.data = data;
            return this;
        }
        public Builder attach(Map<String, Object> attach){
            target.attach = attach;
            return this;
        }
        public Result build(){
            return new Result(target);
        }
    }

    private Result(Result origin){
        this.code = origin.code;
        this.message = origin.message;
        this.data = (T) origin.data;
    }
       /* 
    public static class Builder{
        private Result target;
        public Builder(){
            target = new Result();
        }
        
        public Builder code(Integer code){
            target.code = code;
            return this;
        }
        
        public Builder message(String message){
            target.message = message;
            return this;
        }
        
        public <T> Builder data(T data){
            target.data = data;
            return this;
        }
        public Result build(){
            return new Result(target);
        }
    }*/
}
